package com.api.baidudemo;


import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Config;
import android.util.DisplayMetrics;
import android.util.Log;


import java.util.List;
import java.util.logging.Logger;


/**
 * @brief application 注册
 * @details
 */
public class App extends Application {

    private static Context context;
    private static Handler temlistHandler;
    private static Activity curActivity;

    @Override
    public void onCreate() {
        super.onCreate();


        // 判断当前进程是否是本进程 进程会调用多次 创建多个对象
        String processName = getProcessName(this, android.os.Process.myPid());
        if (processName != null) {
            // 判断当前的进程是我们自己app
            boolean defaultProcess = processName.equals("com.anju.smarthome");// 自己的包名
            if (defaultProcess) {
                // 当前应用的初始化
                init();
            }
        }
    }


    // 是否开启日志
    private void initLogger() {

    }

    public String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
            if (procInfo.pid == pid) {
                return procInfo.processName;
            }
        }
        return null;
    }

    /**
     * 初始化进程
     *
     * @brief
     * @details
     */
    private void init() {
        context = getApplicationContext();

        initLogger();

        registerActivityLifecycleCallbacks(lifecycleCallbacks);

    }


    public static Context getContext() {
        return context;
    }

    public static Handler getTemlistHandler() {
        return temlistHandler;
    }

    public static void setTemlistHandler(Handler temlistHandlesr) {
        temlistHandler = temlistHandlesr;
    }




    public static Activity getCurActivity() {
        return curActivity;
    }

    public static void setCurActivity(Activity curActivity) {
        App.curActivity = curActivity;
    }


    private ActivityLifecycleCallbacks lifecycleCallbacks = new ActivityLifecycleCallbacks() {

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity,
                                                Bundle outState) {

        }

        @Override
        public void onActivityResumed(Activity activity) {
            curActivity = activity;
        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }

        @Override
        public void onActivityCreated(Activity activity,
                                      Bundle savedInstanceState) {
            curActivity = activity;
        }
    };

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

    }
}
